
from tqsdk import TqApi, TqAuth, TqSim, TqKq, TqAccount
from common.constants import BrokerType

class account_helper:
    
    account = None
    accountapi = None

    def __init__(self,  web_gui = False) -> None:
        self.tqauth = TqAuth("","")
        # self.tqauth = TqAuth("","")
        self.web_gui = web_gui

    def init_account(self, brokertype: BrokerType = BrokerType.Default):
        if brokertype == BrokerType.GTJA:
            self.account = TqAccount(BrokerType.GTJA.value,"","")
        elif brokertype == BrokerType.GTJA_FZ:
            self.account = TqAccount(BrokerType.GTJA_FZ.value,"","")
        elif brokertype == BrokerType.ZXJT:
            self.account = TqAccount(BrokerType.ZXJT.value,"","")
        elif brokertype == BrokerType.HYQH:
            self.account = TqAccount(BrokerType.HYQH.value,"","")
        elif brokertype == BrokerType.SNOW:
            self.account = TqAccount(BrokerType.SNOW.value, "", "")
        elif brokertype == BrokerType.TQ:
            self.account =TqKq()
        elif brokertype == BrokerType.KQ:
            self.account =TqKq()
        else:
            self.account = TqSim()

    def  init_api(self):
        self.accountapi = TqApi(self.account, self.tqauth, web_gui=self.web_gui )


    def get_simnowapi(self):
        self.init_account("snow")
        self.init_api()
        return self.accountapi

    def get_tqapi(self):
        self.init_account("tq")
        self.init_api()
        return self.accountapi

    def get_gtjaapi(self):
        self.init_account("gtja")
        self.init_api()
        return self.accountapi

    def get_gtja_fangzhenapi(self):
        self.init_account("gtja_fz")
        self.init_api()
        return self.accountapi
    

from tqsdk import TqBacktest
from datetime import date

class backtest_account:
    def __init__(self, strategy_name = "default:", web_gui = False,startdate = date(2022, 1,1), enddate = date(2022,3,29)) -> None:
        self.tqauth = TqAuth("","")
        self.strategy_name = strategy_name
        self.web_gui = web_gui
                
        self.tqsim = TqSim()
        self.backtest = TqBacktest(start_dt=startdate, end_dt=enddate)

    def get_api(self):
        print("策略开始运行:", self.strategy_name)
        return TqApi(self.tqsim, self.tqauth , web_gui=self.web_gui, backtest=self.backtest )
